package com.example.tencentdemo.controller;

import com.example.tencentdemo.entity.ChatGroup;
import com.example.tencentdemo.entity.group_message;
import com.example.tencentdemo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.tencentdemo.service.Impl.ChatGroupImpl;
import com.example.tencentdemo.service.Impl.MessageImpl;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/chatGroup")
public class ChatGroupController {
    @Autowired
    private ChatGroupImpl ChatGroupImpl;

    @Autowired
    private MessageImpl MessageImpl;

    @GetMapping("/test")
    public String Hello(){
        return "Hello";
    }

    @PostMapping("/insertGroup")
    public Result<ChatGroup> insertGroup(@RequestBody ChatGroup ChatGroup){
        try {
            ChatGroupImpl.insertChatGroup(ChatGroup);
            return Result.success(ChatGroup);
        } catch (Exception e) {
            return Result.internalServerError("内部服务器错误,新建群聊失败");
        }
    }

    @GetMapping("/queryGroup/{activity_id}")
    public Result<ChatGroup>  queryGroup(@PathVariable String activity_id){

        try {
            ChatGroup chatGroup = ChatGroupImpl.selectChatGroupById(activity_id);

            if (chatGroup != null) {
                return Result.success(chatGroup);
            } else {
                return Result.notFound("群聊不存在");
            }
        } catch (Exception e) {
            return Result.internalServerError("内部服务器错误,查询群聊失败");
        }
    }

    @DeleteMapping("/deleteGroup")
    public Result<Void> deleteChatGroup(@RequestParam String group_id) {
        System.out.println(group_id);
        try {
            ChatGroupImpl.deleteChatGroupById(group_id);
            return Result.success();
        } catch (Exception e) {
            return Result.internalServerError("内部服务器错误,删除群聊失败");
        }
    }
}
